package me.sunpeng.buffer;

import org.junit.Test;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author sp
 */
public class BufferDemo2 {


    /**
     * 内存映射文件
     */
    @Test
    public void b04() throws IOException {
        final int start = 0;
        final int size = 1024;

        RandomAccessFile raf = new RandomAccessFile("D:\\mine\\dev\\project\\server\\java\\java-study\\java-study\\iofile\\1.txt", "rw");
        FileChannel fc = raf.getChannel();
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, start, size);
        mbb.put(0, (byte) 97);
        mbb.put(1023, (byte) 122);
        raf.close();


    }

    /**
     * 直接缓冲区
     */
    @Test
    public void b03() throws IOException {
        String infile = "D:\\mine\\dev\\project\\server\\java\\java-study\\java-study\\iofile\\1.txt";
        FileChannel finChannel;
        FileChannel foutChannel;
        FileInputStream fin = new FileInputStream(infile);
        finChannel = fin.getChannel();

        String outfile = "D:\\mine\\dev\\project\\server\\java\\java-study\\java-study\\iofile\\2.txt";
        FileOutputStream fout = new FileOutputStream(outfile);
        foutChannel = fout.getChannel();


        //创建直接缓冲区
        ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
        while (true) {
            buffer.clear();

            int r = finChannel.read(buffer);

            if (r == -1) {
                break;
            }
            buffer.flip();
            foutChannel.write(buffer);


        }


    }


    /**
     * 只读缓冲区
     */
    @Test
    public void b02() {

        ByteBuffer buffer = ByteBuffer.allocate(10);

        for (int i = 0; i < buffer.capacity(); i++) {
            buffer.put((byte) i);
        }

        //创建只读缓冲区
        ByteBuffer readonly = buffer.asReadOnlyBuffer();

        for (int i = 0; i < buffer.capacity(); i++) {

            byte b = buffer.get(i);

            buffer.put(i, (byte) (b * 10));

        }

        readonly.position(0);
        readonly.limit(buffer.capacity());
        while (readonly.remaining() > 0) {
            System.out.println(readonly.get());
        }


    }


    /**
     * 缓冲区分片
     */
    @Test
    public void b01() {
        ByteBuffer buffer = ByteBuffer.allocate(10);

        for (int i = 0; i < buffer.capacity(); i++) {
            buffer.put((byte) i);

        }

        //创建子缓冲区
        buffer.position(3);
        buffer.limit(7);
        ByteBuffer slice = buffer.slice();


        //改变子缓冲区内容
        for (int i = 0; i < slice.capacity(); i++) {
            byte b = slice.get(i);

            slice.put(i, (byte) (b * 10));
        }


        buffer.position(0);
        buffer.limit(buffer.capacity());


        while (buffer.remaining() > 0) {
            System.out.println(buffer.get());
        }


    }
}
